package com.nulldev.util.web.HttpServer3.pages.v3;

import com.nulldev.util.data.Variables;

public enum RequestProtocolType {

	/* HTTP */

	HTTP_0_9("HTTP/0.9", "http", true),
	HTTP_1_0("HTTP/1.0", "http", true),
	HTTP_1_1("HTTP/1.1", "http", true),
	HTTP_2_0("HTTP/2.0", "http", true),

	HTTPS_1_1("HTTP/1.1", "https"),
	HTTPS_2_0("HTTP/2.0", "https"),

	/* STTP */

	STTP_0_9("STTP/0.9", "sttp"),

	/* MISC */

	UNKNOWN("N/A", "unknown"),

	;

	private final String protocolDef, scheme;
	private final boolean isHTTP;

	private RequestProtocolType(final String protocolDef, final String scheme) {
		this(protocolDef, scheme, false);
	}

	private RequestProtocolType(final String protocolDef, final String scheme, final boolean isHTTP) {
		this.protocolDef = protocolDef;
		this.scheme = scheme;
		this.isHTTP = isHTTP;
	}

	public String protocolDef() {
		return this.protocolDef;
	}

	public String scheme() {
		return this.scheme;
	}

	public boolean isHTTP() {
		return this.isHTTP;
	}

	public static RequestProtocolType guessProtocolFromVersion(final String requestVersion) {
		return guessProtocolFromVersion(requestVersion, false);
	}

	public static RequestProtocolType guessProtocolFromVersion(final String requestVersion, final boolean isSecure) {
		switch (Variables.requireNonNullObject(requestVersion, "requestVersion")) {
			case "HTTP/0.9":
				return HTTP_0_9;
			case "HTTP/1.0":
				return HTTP_1_0;
			case "HTTP/1.1":
				return (isSecure ? HTTPS_1_1 : HTTP_1_1);
			case "STTP/0.9":
				return STTP_0_9;
			default:
				return UNKNOWN;
		}
	}
}
